/*
 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
 */
package Bankmanagement;

import java.util.ArrayList;

/**
 * BankingTaskManager Class - Manages bank accounts and tasks, implementing BankingAPI.
 * @author ashongtical
 */
public abstract class BankingTaskManager implements BankingAPI {
    private ArrayList<BankAccount> accounts;
    private TaskManager taskManager;

    /**
     * Constructs a new BankingTaskManager.
     */
    public BankingTaskManager() {
        accounts = new ArrayList<>();
        taskManager = new TaskManager();
    }

    /**
     * Creates a new bank account with the specified details.
     * @param accountNumber The unique account number.
     * @param balance The initial balance.
     * @param annualInterestRate The annual interest rate (as a decimal, e.g., 0.05 for 5%).
     * @throws IllegalArgumentException If the account number already exists.
     */
    @Override
    public void createAccount(String accountNumber, double balance, double annualInterestRate) {
        for (BankAccount account : accounts) {
            if (account.getAccountNumber().equals(accountNumber)) {
                throw new IllegalArgumentException("Account " + accountNumber + " already exists.");
            }
        }
        BankAccount newAccount = new BankAccount(accountNumber, balance, annualInterestRate);
        accounts.add(newAccount);
    }

    /**
     * Deposits an amount into the specified account.
     * @param accountNumber The account number.
     * @param amount The amount to deposit.
     * @throws IllegalArgumentException If the account is not found or amount is invalid.
     */
    @Override
    public void deposit(String accountNumber, double amount) {
        BankAccount account = findAccount(accountNumber);
        account.deposit(amount);
        taskManager.addTask("Deposit RMB " + amount + " to Account " + accountNumber);
    }

    /**
     * Withdraws an amount from the specified account.
     * @param accountNumber The account number.
     * @param amount The amount to withdraw.
     * @throws IllegalArgumentException If the account is not found or amount is invalid.
     * @throws IllegalStateException If the withdrawal would drop balance below minimum.
     */
    @Override
    public void withdraw(String accountNumber, double amount) {
        BankAccount account = findAccount(accountNumber);
        account.withdraw(amount);
        taskManager.addTask("Withdraw RMB " + amount + " from Account " + accountNumber);
    }

    /**
     * Retrieves details of the specified account.
     * @param accountNumber The account number.
     * @return The BankAccount object.
     * @throws IllegalArgumentException If the account is not found.
     */
    @Override
    public BankAccount getAccountDetails(String accountNumber) {
        return findAccount(accountNumber);
    }

    /**
     * Adds a task to the task manager.
     * @param task The task description (use '*' for high priority).
     */
    @Override
    public void addTask(String task) {
        taskManager.addTask(task);
    }

    /**
     * Removes a task from the specified priority list.
     * @param priority The priority level ("high" or "low").
     * @param index The index of the task in the list.
     */
    @Override
    public void removeTask(String priority, int index) {
        taskManager.removeTask(priority, index);
    }

    /**
     * Changes the priority of a task.
     * @param priority The current priority level ("high" or "low").
     * @param index The index of the task in the list.
     */
    @Override
    public void changePriority(String priority, int index) {
        taskManager.changePriority(priority, index);
    }

    /**
     * Promotes a high-priority task to a higher position in the list.
     * @param index The index of the task to promote.
     */
    @Override
    public void promoteTask(int index) {
        taskManager.promoteTask(index);
    }

    /**
     * Retrieves the list of high-priority tasks.
     * @return A copy of the high-priority tasks list.
     */
    @Override
    public ArrayList<String> getHighPriorityTasks() {
        return taskManager.getHighPriorityTasks();
    }

    /**
     * Retrieves the list of low-priority tasks.
     * @return A copy of the low-priority tasks list.
     */
    @Override
    public ArrayList<String> getLowPriorityTasks() {
        return taskManager.getLowPriorityTasks();
    }

    /**
     * Finds an account by account number.
     * @param accountNumber The account number to find.
     * @return The BankAccount object.
     * @throws IllegalArgumentException If the account is not found.
     */
    private BankAccount findAccount(String accountNumber) {
        for (BankAccount account : accounts) {
            if (account.getAccountNumber().equals(accountNumber)) {
                return account;
            }
        }
        throw new IllegalArgumentException("Account " + accountNumber + " not found.");
    }
}